package soar.test.json.json;

import org.json.JSONObject;
import org.json.JSONStringer;
import org.json.JSONArray;

public class App
{
    public static void main(String args[])
    {
        new App().run();
    }

    private void run()
    {
        try
        {
            String split = "========================================";
            System.out.println(split);

            parse();
            System.out.println(split);

            generate();
            System.out.println(split);
        }
        catch (Exception e)
        {
            System.out.println(e);
        }
    }

    private void parse()
    {
        String s;
        JSONObject j;
        JSONArray a;

        s = "{     \"list of lists\" : [         [1, 2, 3],         [4, 5, 6],     ] }";
        j = new JSONObject(s);
        System.out.println(j.toString());

        s = "{ \"entity\": { \"imageURL\": \"\", \"name\": \"IXXXXXXXXXXXXX\", \"id\": 12336, \"ratingCount\": null, \"averageRating\": null } }";
        j = new JSONObject(s);
        System.out.println(j.toString());

        s = "{\"uuid\":100,\"account\":\"壮东风\",\"desc\":null,\"item\":{\"a\":100,\"b\":[\"x\",\"y\",\"z\"]}}";
        j = new JSONObject(s);
        System.out.println(j.toString());
        // 不存在的字段会抛出异常，不能用 == null判断
        // String name=j.getString("name");
        // if(name == null)
        // {
        // System.out.println("name not found");
        // }
        if (j.has("uuid"))
        {
            int uuid = j.getInt("uuid");
            System.out.println("uuid: " + uuid);
        }
        else
            System.out.println("uuid is unavailable");
        if (j.has("account"))
        {
            String account = j.getString("account");
            System.out.println("account: " + account);
        }
        else
            System.out.println("account is unavailable");
        if (j.isNull("notfound"))
        {
            System.out.println("notfound");
        }
        if (j.isNull("desc"))
            System.out.println("desc is null");
        String desc = j.optString("desc");    // null --> ""
        System.out.println(String.format("desc(%d): %s", desc.length(),
                desc));
        JSONObject item = j.optJSONObject("item");
        String ia = item.optString("a");    // 100 --> "100"
        System.out.println("item=>a: " + ia);
        JSONArray ib = item.getJSONArray("b");
        System.out.print("item=>b:");
        for (int i = 0; i < ib.length(); ++i)
        {
            System.out.print(" " + ib.getString(i));
        }
        System.out.println("");

        s = "[\"x\",\"y\",\"z\"]";
        a = new JSONArray(s);
        System.out.println(a.toString());
    }

    private void generate()
    {
        JSONStringer js;

        js = new JSONStringer();
        js.object().key("single").value("MARIE HAA'S").key("Johnny")
                .value("MARIE HAA\\'S").key("foo").value("bar").key("baz")
                .array().object().key("quux").value("Thanks, Josh!")
                .endObject().endArray().endObject();
        System.out.println(js.toString());

        js = new JSONStringer();
        js.array();
        js.value(1);
        js.array();
        js.value(null);
        js.array();
        js.object();
        js.key("empty-array").array().endArray();
        js.key("answer").value(42);
        js.key("null").value(null);
        js.key("false").value(false);
        js.key("true").value(true);
        js.key("big").value(123456789e+88);
        js.key("small").value(123456789e-88);
        js.key("empty-object").object().endObject();
        js.key("long").value(9223372036854775807L);
        js.endObject();
        js.value("two");
        js.endArray();
        js.value(true);
        js.endArray();
        js.value(98.6);
        js.value(-100.0);
        js.object();
        js.endObject();
        js.object();
        js.key("one");
        js.value(1.00);
        js.endObject();
        js.value(100);
        js.endArray();
        System.out.println(js.toString());
        System.out.println(new JSONArray(js.toString()).toString(4));
    }
}
